package springmvc.high.error;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * description: 全局异常处理器
 *
 * 1. @ControllerAdvice 拦截全局异常，交由类中的Handler处理，可以返回逻辑视图，转发和重定向
 * 2. @RestControllerAdvice = @ResponseBody + @ControllerAdvice 直接返回json字符串
 * 3. @ExceptionHandler 指定可以处理异常的Handler，精准匹配或匹配异常的父类
 */

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ArithmeticException.class)
    public Object ArithmeticExceptionHandler(ArithmeticException e){
        //自定义处理异常即可 handler
        String message = e.getMessage();
        System.out.println("message = " + message);
        return message;
    }

    @ExceptionHandler(Exception.class)
    public Object ExceptionHandler(Exception e){
        //自定义处理异常即可 handler
        String message = e.getMessage();
        System.out.println("message = " + message);
        return  message;
    }
}
